//
//  AddressView.m
//  翼安保
//
//  Created by duzhi on 2024/4/23.
//

#import "AddressView.h"

@interface AddressView()<UIPickerViewDelegate,UIPickerViewDataSource>
@property (nonatomic, strong) UIPickerView  *pickerView;
@property (nonatomic, strong) UIButton  *canceButton;
@property (nonatomic, strong) UILabel   *titleL;
@property (nonatomic, strong) UIButton  *finishButton;
@end

@implementation AddressView
- (instancetype)initWithFrame:(CGRect)frame {
    if(self = [super initWithFrame:frame]) {
        [self creatUI];
    }
    return self;
}

- (void)creatUI {
    self.backgroundColor = [[UIColor whiteColor] colorWithAlphaComponent:0.5];
    
    self.canceButton = [UIButton new];
    [self.canceButton setTitle:@"取消" forState:0];
    [self.canceButton setTitleColor:[UIColor blackColor] forState:0];
    self.canceButton.titleLabel.font = [UIFont systemFontOfSize:relativeWidth(16)];
    [self addSubview:self.canceButton];
    [self.canceButton addTarget:self action:@selector(clickCance) forControlEvents:UIControlEventTouchUpInside];
    [self.canceButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(0);
        make.left.mas_equalTo(relativeWidth(15));
        make.top.mas_equalTo(relativeWidth(10));
        make.height.mas_equalTo(relativeWidth(40));
    }];
    
    self.finishButton = [UIButton new];
    [self.finishButton setTitle:@"确定" forState:0];
    [self.finishButton setTitleColor:[UIColor blackColor] forState:0];
    self.finishButton.titleLabel.font = [UIFont systemFontOfSize:relativeWidth(16)];
    [self addSubview:self.finishButton];
    [self.finishButton addTarget:self action:@selector(clickFinish) forControlEvents:UIControlEventTouchUpInside];
    [self.finishButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.centerY.mas_equalTo(self.canceButton);
        make.right.mas_equalTo(relativeWidth(-15));
    }];
    
    self.titleL = [UILabel new];
    self.titleL.font = [UIFont boldSystemFontOfSize:relativeWidth(18)];
    [self addSubview:self.titleL];
    [self.titleL mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(0);
        make.centerY.mas_equalTo(self.canceButton);
    }];
    
    self.pickerView = [UIPickerView new];
    self.pickerView.delegate = self;
    [self addSubview:self.pickerView];
    [self.pickerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.mas_equalTo(0);
        make.top.mas_equalTo(self.canceButton.mas_bottom);
    }];
}

- (void)setDatas:(NSArray<AddressModel *> *)datas {
    _datas = datas;
    [self.pickerView reloadAllComponents];
    self.titleL.text = [NSString stringWithFormat:@"%@%@%@",self.datas[0].Description,self.datas[0].children[0].Description,self.datas[0].children[0].children[0].Description];
}

- (void)clickCance {
    [UIView animateWithDuration:0.3 delay:0 options:UIViewAnimationOptionCurveEaseIn animations:^{
        self.frame = CGRectMake(0, ScreenHeight, ScreenWidth, self.frame.size.height);
    } completion:nil];
}

- (void)clickFinish {
    [UIView animateWithDuration:0.3 delay:0 options:UIViewAnimationOptionCurveEaseIn animations:^{
        self.frame = CGRectMake(0, ScreenHeight, ScreenWidth, self.frame.size.height);
    } completion:^(BOOL finished) {
        if(self.confirmBlock) {
            long index0 = [self.pickerView selectedRowInComponent:0];
            long index1 = [self.pickerView selectedRowInComponent:1];
            long index2 = [self.pickerView selectedRowInComponent:2];
            self.confirmBlock(self.datas[index0],self.datas[index0].children[index1], self.datas[index0].children[index1].children[index2]);
        }
    }];
}

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
    return 3;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
    //@try是因为同事滑动两列必崩
    @try {
        if(component == 0) {
            return self.datas.count;
        }
        if(component == 1) {
            long index = [pickerView selectedRowInComponent:0];
            return self.datas[index].children.count;
        }
        if(component == 2) {
            long index1 = [pickerView selectedRowInComponent:0];
            long index2 = [pickerView selectedRowInComponent:1];
            return self.datas[index1].children[index2].children.count;
        }
        return 0;
    } @catch (NSException *exception) {
        return 0;
    }
}

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
    @try {
        if(component == 0) {
            return self.datas[row].Description;
        }
        if(component == 1) {
            long index = [pickerView selectedRowInComponent:0];
            return self.datas[index].children[row].Description;
        }
        if(component == 2) {
            long index1 = [pickerView selectedRowInComponent:0];
            long index2 = [pickerView selectedRowInComponent:1];
            return self.datas[index1].children[index2].children[row].Description;
        }
        return @"";
    } @catch (NSException *exception) {
        return @"";
    }
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
    if(component == 0) {
        [pickerView reloadComponent:1];
        [pickerView reloadComponent:2];
    }
    if(component == 1) {
        [pickerView reloadComponent:2];
    }
    long index0 = [self.pickerView selectedRowInComponent:0];
    long index1 = [self.pickerView selectedRowInComponent:1];
    long index2 = [self.pickerView selectedRowInComponent:2];
    @try {
        self.titleL.text = [NSString stringWithFormat:@"%@%@%@",self.datas[index0].Description,self.datas[index0].children[index1].Description,self.datas[index0].children[index1].children[index2].Description];
    } @catch (NSException *exception) {
    }
}

@end

